/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;

import cs4280.bean.*;
import cs4280.dao.*;
/**
 *
 * @author PangPei
 */
public class movieRetrieve extends HttpServlet {

    public void sortMovie(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException{
        
        String sort=request.getParameter("sortStrategy");
        if(sort.equals("Genre"))
            sort="genere";
        String order=request.getParameter("order");
        String mpp=request.getParameter("numberPerPage");
        
        MovieDAO movieDAO = new MovieDAO();
        request.setAttribute("movies", movieDAO.getSortMovies(sort,order,mpp));
        
    }

    public void searchMovie(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException{
        ArrayList<Movie> movies=new ArrayList<Movie>();
        ArrayList<Movie> allmovies=new ArrayList<Movie>();
        MovieDAO movieDAO = new MovieDAO();
        allmovies=movieDAO.getMovies();
        String keyword=request.getParameter("keyword");
        if(keyword!=null&&keyword.length()>0)
        {
            
            movies=movieDAO.getSearchMovies(keyword);
            
        }
        request.setAttribute("movies", movies);
        request.setAttribute("allmovies",allmovies);
        request.setAttribute("keyword", keyword);

    }


    public void retrieveMovie(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        Movie movie = new Movie();
        MovieDAO movieDAO = new MovieDAO();
        ScreeningDAO screeningDAO=new ScreeningDAO();
        int movieid=Integer.parseInt(request.getParameter("movieid"));
        movie=movieDAO.getMovie(movieid);
        request.setAttribute("movie", movie);
        ArrayList<Screening> screenings=(ArrayList<Screening>)screeningDAO.getOneMovieScreenings(movieid);
        request.setAttribute("screenings", screenings);
    }

}
